#include "userdelete.h"
#include "ui_userdelete.h"
#include <QMessageBox>
#include <QJsonObject>

UserDeleteWindow::UserDeleteWindow(QWidget *parent)
    : QWidget(parent)
    , userdeletewindowUi(new Ui::UserDeleteWindow)
    , networkManager(new NetworkManager(this))
{
    userdeletewindowUi->setupUi(this);

    connect(networkManager, &NetworkManager::deleteUserResponseReceived, this, &UserDeleteWindow::handleDeleteUserResponse);
}

UserDeleteWindow::~UserDeleteWindow()
{
    delete userdeletewindowUi;
}

void UserDeleteWindow::on_deleteButton_clicked()
{
    QString userName = userdeletewindowUi->userNameLineEdit->text();

    if (userName.isEmpty()) {
        QMessageBox::warning(this, "输入错误", "请填写用户名。");
        return;
    }

    QJsonObject json;
    json["username"] = userName;

    networkManager->deleteUser(json);
}

void UserDeleteWindow::handleDeleteUserResponse(const QJsonObject &response)
{
    int code = response.value("code").toInt();
    QString desc = response.value("desc").toString();

    if (code == 200) {
        QMessageBox::information(this, "用户删除成功！", desc);
    } else {
        QMessageBox::warning(this, "用户删除失败：", desc);
    }
}

void UserDeleteWindow::on_resetbutton_clicked()
{
    userdeletewindowUi->userNameLineEdit->clear();
}
